package com.example.springboot.controller;

import com.example.springboot.dao.admin_menuMapper;
import com.example.springboot.model.RtPageInfo;
import com.example.springboot.model.admin_menu;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.spring5.context.SpringContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class admin_menuController {
    @Autowired
    admin_menuMapper admin_menuMapper;
    @RequestMapping("/addmenu")
    public String list(admin_menu c) throws Exception {
        admin_menuMapper.save(c);
        return "redirect:list";
    }

    @RequestMapping("/list")
    public String listCategory(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "sizxe", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        List<admin_menu> cs=admin_menuMapper.findAll();
        PageInfo<admin_menu> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "admin/menu/list";
    }

    //查询
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public RtPageInfo query(
            @RequestParam(required = false, name = "pageSize") Integer pageSize,
            @RequestParam(required = false, name = "startIndex") Integer startIndex,
            @RequestParam(required = false, name = "pageIndex") Integer pageIndex,
            @RequestParam(required = false, name = "type") Integer type,
            @RequestParam(required = false, name = "name") String name,
            HttpServletRequest req){
        //这里做查询操作
        //验证排序内容
        //int issort=Integer.parseInt(req.getParameter("order[0][column]")) ;
        //String  sortname=req.getParameter("order[0][dir]") ;
        PageHelper.startPage(startIndex,pageSize,"id desc");
        //正常无条件检索
        //List<admin_menu> cs=admin_menuMapper.findAll();
        //多条件检索 使用map K-V参数形式
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("type",type);
        map.put("name",name);
        List<admin_menu> cs=admin_menuMapper.FindAllWhere(map);
        PageInfo<admin_menu> page = new PageInfo<>(cs);
        //返回DataTable使用
        RtPageInfo pageInfo = new RtPageInfo();
        pageInfo.setData(page.getList());//这里是数据内容 List
        pageInfo.setPageNum(startIndex/pageSize);//Integer
        pageInfo.setPageSize(pageSize);//pageSize
        pageInfo.setTotalCount(page.getTotal());//BigInteger
        return pageInfo ;
    }
    /*
    * 菜单添加页
    * */
    @RequestMapping("/menu/add")
    public String  Add(){
        return "admin/menu/add";
    }


    @RequestMapping(value = "/menu/additem",method = RequestMethod.POST)
    @ResponseBody
    public String  AddItem(@RequestParam(value = "name") String name,
                           @RequestParam(value = "url") String url,
                           @RequestParam(value = "icon") String icon,
                           @RequestParam(value = "remarks") String remarks,
                           @RequestParam("file") MultipartFile file,
                           HttpServletRequest request) throws IOException {
        admin_menu a=new admin_menu();
        a.setAddtime(new Date());
        a.setName(name);
        a.setUrl(url);
        a.setIcon(icon);
        a.setRemarks(remarks);
        //file
        if (!file.isEmpty()) {
            //保存时的文件名
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Calendar calendar = Calendar.getInstance();
            String dateName = df.format(calendar.getTime())+file.getOriginalFilename();
            System.out.println(dateName);
           //获取保存路径
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            type = fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
            String realPath = request.getSession().getServletContext().getRealPath("/");
            String trueFileName = String.valueOf(System.currentTimeMillis()) + "." + type;
            // 设置存放图片文件的路径
            String path = realPath+/*System.getProperty("file.separator")+*/trueFileName;
            System.out.println("url:"+path);
            // 转存文件到指定的路径
            file.transferTo(new File(path));
            System.out.println("文件成功上传到指定目录下");
        }
        admin_menuMapper.save(a);
        return "{\"message\":\"ok\"}";
    }
}
